package com.plian.system.controller.lg;


import com.plian.Tools.FileTools;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.dto.lg.LawDisputeDTO;
import com.plian.system.dto.lg.LawDisputeSummaryDTO;
import com.plian.system.entity.lg.LawDispute;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.lg.ILawDisputeService;
import com.plian.system.service.lg.ILawDisputeSummaryService;
import com.plian.system.vo.lg.*;
import com.plian.system.vo.report.ChartsVO;
import com.plian.system.vo.report.PieChartVO;
import com.plian.system.vo.report.ReportReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 法律纠纷 前端控制器
 * </p>
 *
 * @author hu song
 * @since 2023-01-30
 */
@Api("法律纠纷 控制器")
@RestController
@RequestMapping("/api/law-dispute")
@RequiredArgsConstructor
public class LawDisputeController {

    private final ILawDisputeService lawDisputeService;
    @Resource
    private ILawDisputeSummaryService lawDisputeSummaryService;

    @ApiOperation("分页查询")
    @PostMapping("/list")
    public ListResult<LawDisputeVO> list(@RequestBody Query query) {
        return lawDisputeService.findAll(query);
    }

    @ApiOperation("重大案件列表")
    @PostMapping("/majorCases")
    public ListResult<LawDisputeVO> majorCases(@RequestBody Query query) {
        return lawDisputeService.findMajorCases(query);
    }

    @ApiOperation("详情")
    @PostMapping("/detail")
    public R<LawDisputeVO> detail(@RequestBody LawDispute lawDispute) {
        return R.data(lawDisputeService.detail(lawDispute));
    }

    @ApiOperation(value = "新增或修改")
    @PostMapping("/submit")
    @LogOperation("法律纠纷-提交")
    public R submit(@Validated @RequestBody LawDispute lawDispute) {
        return lawDisputeService.saveOrUpdate(lawDispute) ? R.data(lawDispute) : R.fail();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/remove")
    @LogOperation("法律纠纷-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        lawDisputeService.deleteChildren(ids);
        return lawDisputeService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))) ? R.success() : R.fail();

    }

    /**
     * 企业法律纠纷案件情况汇总
     *
     * @param lawDisputeDTO
     * @return
     */
    @ApiOperation(value = "企业法律纠纷案件情况汇总")
    @PostMapping("/lawDisputeSummary")
    public R<ReportReturnVO> lawDisputeSummary(@RequestBody LawDisputeDTO lawDisputeDTO) {
        ReportReturnVO reportReturnVO = lawDisputeService.lawDisputeSummary(lawDisputeDTO);
        R<ReportReturnVO> results = new R<>(CommonCode.SUCCESS);
        results.setPageData(reportReturnVO);
        return results;
    }

    @GetMapping("/buildpdf")
    @PassToken
    @ApiOperation(value = "打印pdf")
    public ResponseEntity<FileSystemResource> buildpdf(HttpServletRequest request) {
        String fillingUnitId=request.getParameter("fillingUnitId");
        String timeSpanId=request.getParameter("timeSpanId");
        String year=request.getParameter("year");

        LawDisputeSummaryDTO lawDisputeDTO=new LawDisputeSummaryDTO();
        List<String> list=new ArrayList<>();
        if(StringUtil.isNotBlank(fillingUnitId)){
            list.add(fillingUnitId);
            lawDisputeDTO.setFillingUnitIds(list);
        }
        if(StringUtil.isNotBlank(timeSpanId)){
            lawDisputeDTO.setTimeSpanId(timeSpanId);
        }
        if(StringUtil.isNotBlank(year)){
            lawDisputeDTO.setYear(year);
        }
        ReportReturnVO vo= lawDisputeSummaryService.lawDisputeSummary(lawDisputeDTO);
        List<LawDisputeSummaryVO> list1=vo.getGrids().getData();
        list1.get(0);
//        LawDisputeSummaryVO lawDisputeSummaryVO=new LawDisputeSummaryVO();
        ResponseEntity<FileSystemResource> resourceResponseEntity=FileTools.exportPdf(lawDisputeService.buildpdf(list1.get(0)));
        return resourceResponseEntity;
    }

    @GetMapping("/totalCases")
    @PassToken
    @ApiOperation(value = "案件总计")
    public R<TotalCasesVO> totalCases(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.totalCases(year, unitId));
    }

    @GetMapping("/proportionOfCases")
    @PassToken
    @ApiOperation(value = "案件构成占比")
    public R<PieChartVO> proportionOfCases(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.proportionOfCases(year, unitId));
    }

    @GetMapping("/rankingOfCases")
    @PassToken
    @ApiOperation(value = "案件构成排名")
    public R<List<RankingOfCasesVO>> rankingOfCases(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.rankingOfCases(year, unitId));
    }

    @GetMapping("/proportionOfAmountInvolved")
    @PassToken
    @ApiOperation(value = "涉案金额占比")
    public R<PieChartVO> proportionOfAmountInvolved(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.proportionOfAmountInvolved(year, unitId));
    }

    @GetMapping("/rankingOfUnit")
    @PassToken
    @ApiOperation(value = "单位排名(前十名)")
    public R<List<RankingOfUnitVO>> rankingOfUnit(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.rankingOfUnit(year, unitId));
    }

    @GetMapping("/caseTrends")
    @PassToken
    @ApiOperation(value = "案件趋势(过去5年)")
    public R<ChartsVO> caseTrends(@RequestParam String year, @RequestParam String unitId){
        return R.data(lawDisputeService.caseTrends(year, unitId));
    }
}

